package com.netease.framework.skin;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;

public class InternalSkinResources implements IResources {
	private static final String SUFFIX = "_black";
	private Context mContext;
	private Resources mRes;

	public InternalSkinResources(Context ctx) {
		mContext = ctx.getApplicationContext();
		mRes = mContext.getResources();
	}

	@Override
	public Drawable getDrawable(int id) throws Resources.NotFoundException {
		int k = -1;
		k = getId(id);

		if (k <= 0) {
			return null;
		}
		return mRes.getDrawable(k);
	}

	@Override
	public int getColor(int id) throws Resources.NotFoundException {
		int k = -1;
		k = getId(id);

		if (k <= 0) {
			throw new Resources.NotFoundException("no resource found for: "
					+ id);
		}

		return mRes.getColor(k);
	}

	protected String getResourceEntryName(String name) {
		String pName = name + SUFFIX;
		return pName;
	}

	private int getId(int id) {
		String resTypeName = mRes.getResourceTypeName(id);
		String resEntryName = mRes.getResourceEntryName(id);

		String newName = getResourceEntryName(resEntryName);
		int newId = mRes.getIdentifier(newName, resTypeName, mContext.getPackageName());
	
		return newId;
	}

	public int getIdentifier(int id) {
		return getId(id);
	}

	@Override
	public ColorStateList getColorStateList(int id) throws NotFoundException {
		int k = -1;
		k = getId(id);

		if (k <= 0) {
			throw new Resources.NotFoundException("no resource found for: " + id);
		}

		return mRes.getColorStateList(k);
	}
}
